package com.liziyi0914.morearmor.effect;

import com.liziyi0914.morearmor.Utils;
import com.liziyi0914.morearmor.event.Listener;
import com.liziyi0914.morearmor.event.entity.EntityInteractEntityEvent;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsage;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;

import java.util.function.Predicate;

public class MushroomStewEffect extends AbstractEffect {

    @Listener
    public void onClickEntity(EntityInteractEntityEvent e) {
        if (e.getTarget() == null) return;
        if (!(e.getTarget() instanceof ServerPlayerEntity)) {
            return;
        }
        if (!predicateArmor().test(e.getTarget())) {
            return;
        }
        ServerPlayerEntity player = (ServerPlayerEntity) e.getEntity();
        ItemStack itemStack = player.getMainHandStack();

        if (!itemStack.isOf(Items.BOWL)) {
            return;
        }

        ItemStack itemStack3 = new ItemStack(Items.MUSHROOM_STEW);

        ItemStack itemStack4 = ItemUsage.exchangeStack(itemStack, player, itemStack3, false);
        player.setStackInHand(Hand.MAIN_HAND, itemStack4);

        e.getEntity().world.playSound(null, player.getBlockPos(), SoundEvents.ENTITY_MOOSHROOM_MILK, SoundCategory.PLAYERS, 1.0F, 1.0F);
    }

    @Override
    public Predicate<LivingEntity> predicateArmor() {
        return Utils.hasItemArmor(Items.MUSHROOM_STEW);
    }
}
